package com.zhb.sys.shiro.user.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.zhb.core.constant.RetCode;
import com.zhb.core.utils.JsonReturn;
import com.zhb.core.utils.UUIDUtil;
import com.zhb.sys.shiro.model.User;
import com.zhb.sys.shiro.user.service.UserService;

/**
 * @ClassName: Usercontroller。
 * @Description: User管理Controller
 * @author: zhb
 *
 */
@RestController
@RequestMapping("/user/")
public class UserController {
	@Autowired
	private UserService userService;

	@RequestMapping("userInfo")
	public ModelAndView userInfo(User user, HttpServletRequest request) {
		List<User> users = userService.selectUserByCondition(user);
		return new ModelAndView("shiro/user/userInfo").addObject("userList", users);
	}

	@RequestMapping("insert")
	public JsonReturn insert(User user) {
		JsonReturn jsonReturn = new JsonReturn();
		// 设置ID
		user.setId(UUIDUtil.getId());
		int flag = userService.insert(user);
		if (flag == 1) {
			jsonReturn.setRetcode(RetCode.OK_CODE);
			jsonReturn.setRetmsg("添加成功");
		} else {
			jsonReturn.setRetcode(RetCode.ERROR_CODE);
			jsonReturn.setRetmsg("添加失败");
		}
		return jsonReturn;
	}

	@RequestMapping("selectUserById")
	public JsonReturn selectUserById(String id, HttpServletRequest request) {
		JsonReturn jsonReturn = new JsonReturn();
		User user = userService.selectUserById(id);
		jsonReturn.setRetcode(RetCode.OK_CODE);
		jsonReturn.setRetmsg(user);
		return jsonReturn;
	}

	@RequestMapping("edit")
	public JsonReturn edit(User user) {
		JsonReturn jsonReturn = new JsonReturn();
		// 设置ID
		int flag = userService.update(user);
		if (flag == 1) {
			jsonReturn.setRetcode(RetCode.OK_CODE);
			jsonReturn.setRetmsg("修改成功");
		} else {
			jsonReturn.setRetcode(RetCode.ERROR_CODE);
			jsonReturn.setRetmsg("修改失败");
		}
		return jsonReturn;
	}

	@RequestMapping("delete")
	public JsonReturn delete(String id) {
		JsonReturn jsonReturn = new JsonReturn();
		// 设置ID
		int flag = userService.delete(id);
		if (flag == 1) {
			jsonReturn.setRetcode(RetCode.OK_CODE);
			jsonReturn.setRetmsg("删除成功");
		} else {
			jsonReturn.setRetcode(RetCode.ERROR_CODE);
			jsonReturn.setRetmsg("删除失败");
		}
		return jsonReturn;
	}
}